﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ZLCMS.Business.Util;
using ZLCMS.Business.WeixinService;
using Senparc.Weixin.MP.AdvancedAPIs;
using NHibernate;
using DotNet.Common.NHibernateUtil;

namespace ZLCMS.Web.Wap.Code
{
    public class WeixinPageHelper
    {
        public WeixinPageHelper(HttpContext context)
        {
            Context = context;
        }

        public HttpContext Context { get; set; }

        public string GetOpenId()
        {
            string openid = "";
            //授权
            if (!string.IsNullOrEmpty(Context.Request["code"]))
            {

                //OAuth2Util oAuth2 = new OAuth2Util();
                //OAuth2BaseResponse baseInfo = oAuth2.GetOAuth2Response<OAuth2BaseResponse>(Context.Request["code"]);
                //if (baseInfo != null)
                //{
                //    Context.Session[GlobalSessionString.OauthResponseCode] = Context.Request["code"];
                //    if (string.IsNullOrEmpty(baseInfo.ErrCode))
                //    {
                //        openid = baseInfo.OpenId;
                //    }
                //    else
                //    {
                //        Context.Response.Write("<script>alert('用户信息获取失败!code：" + Context.Request["code"] + " 错误码:" + baseInfo.ErrCode + " 错误消息:" + baseInfo.ErrMsg + "');</script>");
                //        Context.Response.End();
                //    }
                //}
                //else
                //{
                //    Context.Response.Write("<script>alert('授权请求失败!');</script>");
                //    Context.Response.End();
                //}
            }
            else
            {
                openid = Context.Request["openid"];
            }

            return openid;
        }

        public void Jump2OAuthPage()
        {
            //跳转到授权页面
            string url = OAuth.GetAuthorizeUrl(WeixinUtil.GetAppid("admin"), ServerUtil.GetRootURI() + Context.Request.RawUrl, "myself", OAuthScope.snsapi_base);
            Context.Response.Redirect(url);
        }
    }
}